import modelExtend from 'dva-model-extend'
import {queryGridData, queryGridDef} from '../process/service'
import {commonFlowModel} from "../common/commonFlowModel";
import {getStepsToCreateProcess} from "../common/modal/modalContentUtils";
import {popupModalToCreate} from "../common/modal/modalPropertyUtils";
import {dispatchInSetup} from "../common/utils/ModelUtils";


export default modelExtend(
  commonFlowModel, {

    namespace: "process",
    subscriptions: {
      setup({dispatch, history}) {
        history.listen((location) => {
          if (location.pathname === '/flowConfig/process' ) {
            dispatchInSetup(dispatch, location)
          }
        })
      },
    },

    effects: {
      * query({
                payload
              }, {call, put}) {

        const viewConfig = yield call(queryGridDef, payload)
        const data = yield call(queryGridData, payload)
        yield put({
          type: 'querySuccess',
          payload: {
            rawData: data,
            rawConfig: viewConfig,
          },
        })
      },
    },

    reducers: {
      showModalToCreate: function (state) {
        let steps = getStepsToCreateProcess();
        let modalTitle="Create Process";

        popupModalToCreate(state.modalOptions, modalTitle, steps)

        return {...state}
      },
    }


  })


